package com.dhcc.core.modules.system.controller;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.Bean2MapUtil;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.cache.user.UserCache;
import com.dhcc.core.modules.system.entity.Notice;
import com.dhcc.core.modules.system.service.INoticeService;
import com.dhcc.core.modules.system.warpper.NoticeWrapper;


/**
 * 通知表
 * 
 * @ClassName: NoticeController
 * @Description: TODO
 * @author cyf
 * @date 2018-03-15 20:06:56
 */
@Controller
@RequestMapping("${sys.backendPath}/notice")
public class NoticeController extends BaseController {

	private String PREFIX = "/backend/system/notice/";

	@Autowired
	private INoticeService noticeService;
	

    /**
     * 跳转到通知表列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "notice.html";
    }
	
    /**
     * 跳转到添加通知表
     */
    @Permission("notice:add")
    @RequestMapping("/to_add")
    public String noticeAdd() {
        return PREFIX + "notice_add.html";
    }

    /**
     * 跳转到修改通知表
     */
    @Permission("notice:update")
    @RequestMapping("/to_update/{noticeId}")
    public String noticeUpdate(@PathVariable Long noticeId, Model model) {
        Notice notice = this.noticeService.selectById(noticeId);
        model.addAttribute("notice",notice);
        return PREFIX + "notice_edit.html";
    }
    /**
     * 跳转到修改通知表
     */
    @RequestMapping("/view/{noticeId}")
    public String noticeView(@PathVariable Long noticeId, Model model) {
        Notice notice = this.noticeService.selectById(noticeId);
        Map<String, Object> map = Bean2MapUtil.toMap(notice);
        map.put("createByName", UserCache.me().getUserNameById(notice.getCreateBy()));
        model.addAttribute("notice",map);
        return PREFIX + "notice_view.html";
    }

    /**
     * 获取通知表列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<Notice> page = new PageFactory<Notice>().defaultPage();
        List<Notice> result = this.noticeService.list(page,params);
        page.setRecords((List<Notice>) new NoticeWrapper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增通知表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("notice:add")
    @BussinessLog(value = "新增通知表")
    public Object add(Notice notice) {
        if (CommonUtil.isOneEmpty(notice)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        notice.insert();
        return R.ok();
    }

    /**
     * 删除通知表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("notice:delete")
    @BussinessLog(value = "删除通知表")
    public Object delete(@RequestParam Long noticeId) {
	
        this.noticeService.deleteById(noticeId);

        return R.ok();
    }

    /**
     * 修改通知表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("notice:update")
    @BussinessLog(value = "修改通知表")
    public Object update(Notice notice) {
        if (CommonUtil.isOneEmpty(notice)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        Notice old = this.noticeService.selectById(notice.getId());

        old.setTitle(notice.getTitle());
        old.setType(notice.getType());
        old.setContent(notice.getContent());
        old.updateById();
        return R.ok();
    }
	
}
